package com.walter.learn.UseThread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @ProjectName: learnOwner
 * @Package: com.walter.learn.UseThread
 * @ClassName: TestUseThreadPool
 * @Description: java类作用描述
 * @Author: 唐朝
 * @CreateDate: 2019/2/19 9:19
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/2/19 9:19
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class TestUseThreadPool {
    static volatile int i=0;
    public static void main(String[] args) {
        // 创建一个可重用固定线程数的线程池
        ExecutorService pool = Executors.newFixedThreadPool(2);
        // 创建实现了Runnable接口对象，Thread对象当然也实现了Runnable接口
        Thread t1 = new MyThread();
        Thread t2 = new MyThread2();
        // 将线程放入池中进行执行
        pool.execute(t1);
        pool.execute(t2);
        pool.shutdown();
    }

    static class MyThread extends Thread {
        @Override
        public void run() {
            i=i+1;
            System.out.println(Thread.currentThread().getName() + "正在执行… …"+i);
            try {
                Thread.sleep(1l);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    static class MyThread2 extends Thread {
        @Override
        public void run() {
            i=i-1;
            System.out.println(Thread.currentThread().getName() + "正在执行… …"+i);
        }
    }
}
